package mall.controller;

import mall.core.session.SessionKey;
import mall.core.web.Controller;
import mall.entity.Image;
import mall.entity.User;
import mall.repository.MenuRepository;
import mall.service.ImageService;
import mall.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

import static mall.core.session.SessionKey.LOGIN_USER;

/**
 * @author xf07b1
 * @wechat xf07b1
 * @description
 */
@WebServlet("/public")
public class PublicController extends Controller {
    private final UserService userService = new UserService();
    private final ImageService imageService = new ImageService();
    private final MenuRepository menuRepository = new MenuRepository();

    /**
     * 注册
     */
    public void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        User user = userService.getByUsername(username);

        if (user != null) {
            request.setAttribute("message", "用户名已存在");
            forward("/register", request, response);
            return;
        }

        user = new User();
        user.setName("");
        user.setUsername(username);
        user.setPassword(password);
        user.setRole(0);
        userService.add(user);

        forward("/login", request, response);
    }

    /**
     * 登录
     */
    public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        User user = userService.getByUsername(username);

        if (user == null || !password.equals(user.getPassword())) {
            request.setAttribute("message", "用户名或密码错误");
            request.setAttribute("username", username);
            request.setAttribute("password", password);
            forward("/login", request, response);
            return;
        }

        request.getSession().setAttribute("menuList", menuRepository.selectList());
        request.getSession().setAttribute(LOGIN_USER, user);

        if (user.getRole() == 0) {
            redirect("/index", request, response);
        } else {
            redirect("/admin/product?method=viewList", request, response);
        }

    }

    /**
     * 退出
     */
    public void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getSession().removeAttribute(SessionKey.LOGIN_USER);
        redirect("/index", request, response);
    }


    /**
     * 访问注册页面
     */
    public void viewRegister(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        forward("/register", request, response);
    }

    /**
     * 访问登录页面
     */
    public void viewLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        forward("/login", request, response);
    }

    /**
     * 下载图片
     */
    public void image(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        Image image = imageService.download(id);
        InputStream inputStream = image.getData();
        ServletOutputStream outputStream = response.getOutputStream();

        byte[] buffer = new byte[4096];
        int bytesRead = -1;

        while ((bytesRead = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, bytesRead);
        }

        inputStream.close();
        outputStream.flush();
        outputStream.close();
    }
}
